/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.MtItem;
import com.zhuoyue.elmt.service.MtItemService;

/**
 * 电梯维保项目查询Controller
 * @author chenhao
 * @version 2016-09-07
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/mtItem")
public class MtItemController extends BaseController {

	@Autowired
	private MtItemService mtItemService;
	
	@ModelAttribute
	public MtItem get(@RequestParam(required=false) String id) {
		MtItem entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mtItemService.get(id);
		}
		if (entity == null){
			entity = new MtItem();
		}
		return entity;
	}
	
	@RequiresPermissions("elmt:mtItem:view")
	@RequestMapping(value = {"list", ""})
	public String list(MtItem mtItem, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MtItem> page = mtItemService.findPage(new Page<MtItem>(request, response), mtItem); 
		model.addAttribute("page", page);
		return "zhuoyue/elmt/mtItemList";
	}

	@RequiresPermissions("elmt:mtItem:view")
	@RequestMapping(value = "form")
	public String form(MtItem mtItem, Model model) {
		model.addAttribute("mtItem", mtItem);
		return "zhuoyue/elmt/mtItemForm";
	}

	@RequiresPermissions("elmt:mtItem:edit")
	@RequestMapping(value = "save")
	public String save(MtItem mtItem, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, mtItem)){
			return form(mtItem, model);
		}
		mtItemService.save(mtItem);
		addMessage(redirectAttributes, "保存维保项目成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/mtItem/?repage";
	}
	
	@RequiresPermissions("elmt:mtItem:edit")
	@RequestMapping(value = "delete")
	public String delete(MtItem mtItem, RedirectAttributes redirectAttributes) {
		mtItemService.delete(mtItem);
		addMessage(redirectAttributes, "删除维保项目成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/mtItem/?repage";
	}

}